package com.eden.manage.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.SecureUtil;

public class LoginUtils {

    public static final String loginTokenKey ="%s-%s";

    public static String generateToken(String source) {
        String currentDate = DateUtil.formatDate(DateUtil.date());
        String combinedSource = source + "|" + currentDate;
        return SecureUtil.sha256(combinedSource);
    }

    public static boolean validateToken(String token, String source, int days) {
        for (int i = 0; i <= days; i++) {
            String pastDate = DateUtil.formatDate(DateUtil.offsetDay(DateUtil.date(), -i));
            String combinedSource = source + "|" + pastDate;
            String expectedToken = SecureUtil.sha256(combinedSource);
            if (token.equals(expectedToken)) {
                return true;
            }
        }
        return false;
    }

}
